package com.baidu.app.controller;

import javax.servlet.http.HttpServletRequest;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baidu.app.service.AppServiceI;
import com.baidu.app.thread.ParserThread;

@Controller
@RequestMapping("app")
public class AppController {
	
	@Autowired
	private AppServiceI service;
	
	
	/**
	 * 
	 * @author 刘鹏
	 * @date 2016年7月7日  下午5:15:53
	 * @function 分配线程抓取应用信息
	 */
	@RequestMapping("htmlParser")
	public void htmlParser() {
		 try {
				
				Parser parser = new Parser("http://mm.10086.cn/android/game/qbyx");
				parser.setEncoding("UTF-8");
				
				HasAttributeFilter hasAttributeFilter = new HasAttributeFilter("class", "last");
				NodeList list = parser.parse(hasAttributeFilter);		
				
				//需要抓取的总页面
				int totalPage= Integer.parseInt(((TagNode)list.elementAt(0)).getAttribute("href").split("=")[1]);
				
				//每个线程的任务量
				int threadPageNum = 10;
				
				//需要线程数量
				int threadNum = totalPage/threadPageNum + (totalPage%threadPageNum==0?0:1);
				
				
				for (int i = 0; i < threadNum; i++) {					
					ParserThread parserThread = new ParserThread(threadPageNum*i+1, threadPageNum*(i+1)>totalPage?totalPage:threadPageNum*(i+1), service);
				    parserThread.start();
				}
						
			 } catch (Exception e) {
				e.printStackTrace();
			 }
	}
	

   
}
